<template>
	<view class="account_conversion">
		<view class="second">
			<view class="ccc">
				<view class="title">认养券</view>{{price.adoptVoucher?price.adoptVoucher:0}}
			</view>
			<view class="ccc">
				<view class="title">鸡蛋</view>{{price.pointsDynamic?price.pointsDynamic:0}}
			</view>
			<view class="ccc">
				<view class="title">余额</view>{{price.cash?price.cash:0}}
			</view>
			<view class="ccc">
				<view class="title">姓名</view>{{name}}
			</view>
		</view>
		<form @submit="formSubmit" @reset="formReset">
			<view class="first">
				<view class="uni-form-item uni-column">
					<view class="title flex">转入ID号：
						<input class="input" type="text" v-model="form.recipientId" @input="handleInput" />
					</view>
				</view>
				<view class="uni-form-item uni-column">
					<view class="title flex">转入数量：
						<input class="input" type="number" v-model="form.recipientMoney" />
					</view>
				</view>
				<view class="uni-form-item uni-column">
					<view class="title flex">账户类型：
						<view @click="fuck(ddd,item.accountType)" v-for="(item,ddd) in accountList" class="uni-input"
							:class="accountIndex===ddd?'bac':''">{{item.name}}</view>
					</view>
				</view>
			</view>
			<button class="uni-btn-v" type="primary" form-type="submit">提交</button>
		</form>
		<paypass ref="yspay" @cancel="handleClose" @submit="handleSubmit"></paypass>
	</view>
</template>

<script>
	import paypass from "@/components/ys-paypass/ys-paypass.vue";
	export default {
		components: {
			paypass
		},
		data() {
			return {
				payPassword: "",
				index: 0,
				visible: false,
				name: "",
				form: {
					recipientMoney: "",
					recipientId: ""
				},
				price: {},
				accountList: [{
					name: "认养券",
					accountType: 1
				}, {
					name: "鸡蛋",
					accountType: 2
				}, {
					name: "余额",
					accountType: 6
				}],
				accountIndex: 0,
				accountType: 1,
			}
		},
		onShow() {
			this.getInit()
		},
		methods: {
			fuck(e, accountType) {
				this.accountIndex = e
				this.accountType = accountType
			},
			getInit() {
				// 查询各类余额
				this.$http({
					url: '/member/getMemberAccount',
				}).then(res => {
					if (res.data.code === 200) {
						this.price = res.data.data
					} else if (res.data.code === 450) {
						uni.showToast({
							title: res.data.message,
							icon: 'error'
						})
						setTimeout(() => {
							uni.navigateTo({
								url: '/pages/Applogin/Applogin'
							})
						}, 1000)
					}
				})
			},
			handleClose() {
				this.payPassword = ""
				this.visible = false
			},
			handleInput() {
				this.$http({
					url: '/member/getMemberName',
					method: "GET",
					data: {
						memberId: this.form.recipientId
					}
				}).then(res => {
					setTimeout(() => {
						if (res.data.code === 200) {
							this.name = res.data.data.memberName
						} else {
							this.name = ""
						}
					}, 100)
				})
			},
			formSubmit() {
				if (!this.form.recipientId) {
					uni.showToast({
						title: '转入编号不能为空',
						icon: 'error'
					})
				} else if (!this.form.recipientMoney) {
					uni.showToast({
						title: '转入数量不能为空',
						icon: 'error'
					})
				} else {
					uni.showModal({
						cancelText: "取消",
						confirmText: "确认",
						title: "确认转账",
						content: '请再次确认所填内容是否正确',
						confirmColor: "#88A58A",
						cancelColor: "#222222",
						success: res => {
							if (res.confirm) {
								this.$refs.yspay.show();
								this.visible = true
							}
						}
					})

				}
			},
			handleSubmit(ex) {
				this.$http({
					url: '/GoldLeaf/shoppingVoucherDonation',
					method: "POST",
					data: {
						...this.form,
						accountType: this.accountType,
						payPassword: ex.value,
					}
				}).then(res => {
					if (res.data.code === 200) {
						uni.showToast({
							title: '转账成功',
						})
						this.visible = false
						this.payPassword = ""
						this.form = {
							recipientMoney: "",
							recipientId: ""
						}
						this.getInit()
					} else {
						uni.showToast({
							title: res.data.message,
							icon: 'error'
						})
					}
				})
			},
			formReset() {

			},
		}
	}
</script>

<style lang="scss" scoped>
	@import './vipTransfer.css'
</style>
